package com.loadmore.common.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.loadmore.common.image.ImageLoader;

/**
 * Created by sunleilei on 2017/3/7
 */

public class RVBaseHolder extends RecyclerView.ViewHolder{

    public View itemView;
    private SparseArray<View> views;
    public RVBaseHolder(View itemView) {
        super(itemView);
        this.itemView = itemView;
        views = new SparseArray<>();
    }

    public <T extends View> T retriece(int id) {
        View view = views.get(id);
        if (view == null) {
            view = itemView.findViewById(id);
            views.put(id,view);
        }
        return (T) view;
    }

    public RVBaseHolder setText(int id,String text) {
        TextView textView = retriece(id);
        if (textView != null) {
            textView.setText(text);
        }
        return this;
    }

    public RVBaseHolder setImage(Context context,int id, String url){
        ImageView imageView = retriece(id);
        ImageLoader.getInstance(context).loadImg(url,imageView);
        return this;
    }
}
